<?php
//=======================================
//###################################
// Kayako Web Solutions
//
// Source Copyright 2001-2004 Kayako Web Solutions
// Unauthorized reproduction is not allowed
// License Number: $%LICENSE%$
// $Author: vshoor $ ($Date: 2006/04/11 20:58:15 $)
// $RCSfile: staff_troubleshooter.php,v $ : $Revision: 1.14 $ 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//###################################
//=======================================


if (!defined("INSWIFT")) {
	trigger_error("Unable to process $PHP_SELF", E_USER_ERROR);
}

if ($_SWIFT["staff"]["tr_entab"] == "0")
{
	echo '<font face="Verdana, Arial, Helvetica" size="2" color="red"><b>'.$_SWIFT["language"]["nopermission"].'</b></font>';
	exit;
}

require_once ("./modules/troubleshooter/functions_troubleshooter.php");
require_once ("./includes/functions_html.php");
require_once ("./includes/functions_template.php");
$template->loadLanguageSection("troubleshooter");

$interface->assignQuickLink($_SWIFT["language"]["managecategories"], "index.php?_m=troubleshooter&_a=managecategories", "icon_list.gif");
$interface->assignQuickLink($_SWIFT["language"]["managesteps"], "index.php?_m=troubleshooter&_a=managesteps", "icon_yellowbigdot.gif");
$interface->assignQuickLink($_SWIFT["language"]["insertcategory"], "index.php?_m=troubleshooter&_a=insertcategory", "icon_newcategory.gif");

renderCategoryList();

/**
* ###############################################
* FUNCTION DECLARATIONS
* ###############################################
*/

/**
* Callback for parsing troubleshotoer data
*/
function _troubleshooterCatFields($arg)
{
	global $_SWIFT, $_sladays;
	$record = &$arg[0];
	$fields = &$arg[1];
	
	$record["title"] = '<table width="100%"  border="0" cellspacing="0" cellpadding="2"><tr><td width="1"><img src="'. $_SWIFT["themepath"] .'icon_folderyellow3.gif" border="0" /></td><td width="100%"><span class="smalltext"><a href="index.php?_m=troubleshooter&_a=editcategory&troubleshootercatid='. $record["troubleshootercatid"] .'"><b>'. $record["title"] .'</b></a></span></td></tr><tr><td colspan="2"><span class="smalltext">'. nl2br($record["description"]) .'</span></td></tr></table>';

	$record["options"] = '<a href="index.php?_m=troubleshooter&_a=editcategory&troubleshootercatid='. $record["troubleshootercatid"] .'" title="'. $_SWIFT["language"]["edit"] .'"><img src="'. $_SWIFT["themepath"] .'icon_edit.gif" border="0">&nbsp;'. $_SWIFT["language"]["edit"] .'</a>&nbsp;&nbsp;&nbsp;&nbsp;<a onClick="javascript:doConfirm(\''. $_SWIFT["language"]["tcatdelconfirm"] .'\', \'index.php?_m=troubleshooter&_a=managecategories&do=delete&troubleshootercatid='. $record["troubleshootercatid"] .'\');" href="#" title="'. $_SWIFT["language"]["delete"] .'"><img src="'. $_SWIFT["themepath"] .'icon_delete.gif" border="0">&nbsp;'. $_SWIFT["language"]["delete"] .'</a>&nbsp;';

	return $record;
}

/**
* Mass Action: Delete Troubleshooter Categories
*/
function _maDeleteTroubleshooterCat()
{
	global $dbCore, $_SWIFT, $infomessage, $errormessage;

	if (_is_array($_POST["itemid"]))
	{
		if ($_SWIFT["staff"]["tsdeletecategory"] == 1)
		{
			$infomessage = $_SWIFT["language"]["tcatsdeleteconfirm"];
			deleteTroubleshooterCat($_POST["itemid"]);
		} else {
			$errormessage = $_SWIFT["language"]["nopermission"];
		}
	}
}

/**
* Callback for parsing troubleshooter step data
*/
function _troubleshooterStepFields($arg)
{
	global $_SWIFT;
	$record = &$arg[0];
	$fields = &$arg[1];

	$record["troubleshootersteps.subject"] = '<img src="'. $_SWIFT["themepath"] .'icon_bluebigdot.gif" border="0" />&nbsp;'.$record["subject"];
	$record["troubleshootersteps.displayorder"] = $record["displayorder"];

	$record["options"] = '<a href="index.php?_m=troubleshooter&_a=editstep&troubleshooterid='. $record["troubleshooterid"] .'" title="'. $_SWIFT["language"]["edit"] .'"><img src="'. $_SWIFT["themepath"] .'icon_edit.gif" border="0">&nbsp;'. $_SWIFT["language"]["edit"] .'</a>&nbsp;&nbsp;&nbsp;&nbsp;<a onClick="javascript:doConfirm(\''. $_SWIFT["language"]["tcatdelconfirm"] .'\', \'index.php?_m=troubleshooter&_a=managestepslist&do=delete&troubleshooterid='. $record["troubleshooterid"] .'\');" href="#" title="'. $_SWIFT["language"]["delete"] .'"><img src="'. $_SWIFT["themepath"] .'icon_delete.gif" border="0">&nbsp;'. $_SWIFT["language"]["delete"] .'</a>&nbsp;';

	return $record;
}

/**
* Mass Action: Delete Troubleshooter Steps
*/
function _maDeleteTroubleshooterStep()
{
	global $dbCore, $_SWIFT, $errormessage;

	if (_is_array($_POST["itemid"]))
	{
		if ($_SWIFT["staff"]["tsdeletestep"] == 1)
		{
			foreach ($_POST["itemid"] as $key=>$val)
			{
				$totaldel = deleteSteps($val);
			}
		} else {
			$errormessage = $_SWIFT["language"]["nopermission"];
		}
	}
}

/**
* Check whether or not html area is required
*/
function checkTRHtmlArea()
{
	global $_SWIFT, $template;
	if ($_SWIFT["settings"]["tr_staffwysiwyg"] == 1)
	{
		$template->assign("htmlarea", true);
	}
}

/**
* Get whether or not html area is required
*/
function getTRHtmlArea()
{
	global $_SWIFT;
	if ($_SWIFT["settings"]["tr_staffwysiwyg"] == 1)
	{
		return "1";
	}
	return "0";
}

/**
* ###############################################
* MANAGE CATEGORIES
* ###############################################
*/
if ($eventaction == "managecategories")
{
	if ($_GET["do"] == "delete" && !empty($_GET["troubleshootercatid"]))
	{
		if ($_SWIFT["staff"]["tsdeletecategory"] == 1)
		{
			$infomessage = $_SWIFT["language"]["tcatdeleteconfirm"];

			deleteTroubleshooterCat(array($_GET["troubleshootercatid"]));
		} else {
			$errormessage = $_SWIFT["language"]["nopermission"];
		}
	}

	if (!empty($_REQUEST["insertcat"]))
	{
		$infomessage = sprintf($_SWIFT["language"]["troubleshootercatinsertconfirm"], htmlspecialchars($_REQUEST["insertcat"]));
	} else if (!empty($_REQUEST["updatecat"])) {
		$infomessage = sprintf($_SWIFT["language"]["troubleshootercatupdateconfirm"], htmlspecialchars($_REQUEST["updatecat"]));
	}
	
	$options["recordsperpage"] = "8";
	$options["sortby"] = "displayorder";
	$options["sortorder"] = "asc";
	$options["massaction"][0]["title"] = $_SWIFT["language"]["delete"];
	$options["massaction"][0]["callback"] = "_maDeleteTroubleshooterCat";

	$options["idname"] = "troubleshootercatid";
	$options["quicksearch"] = true;

	$options["advancedsearch"][0]["title"] = $_SWIFT["language"]["tscattitle"];
	$options["advancedsearch"][0]["query"] = 'SELECT * FROM `'. TABLE_PREFIX .'troubleshootercat` WHERE `title` LIKE \'%$_searchstr%\' $_sortjoin;';

	$options["advancedsearch"][1]["title"] = $_SWIFT["language"]["tscatdesc"];
	$options["advancedsearch"][1]["query"] = 'SELECT * FROM `'. TABLE_PREFIX .'troubleshootercat` WHERE `description` LIKE \'%$_searchstr%\' $_sortjoin;';

	$options["callback"] = "_troubleshooterCatFields";

	$options["toolbar"][0]["title"] = $_SWIFT["language"]["insert"];
	$options["toolbar"][0]["url"] = "index.php?_m=troubleshooter&_a=insertcategory";

	$options["toolbar"][1]["title"] = $_SWIFT["language"]["steps"];
	$options["toolbar"][1]["url"] = "index.php?_m=troubleshooter&_a=managesteps";

	$fields[0]["name"] = "title";
	$fields[0]["title"] = $_SWIFT["language"]["tscattitle"];
	$fields[0]["width"] = "";

	$fields[1]["name"] = "views";
	$fields[1]["title"] = $_SWIFT["language"]["tscatviews"];
	$fields[1]["width"] = "150";
	$fields[1]["align"] = "center";

	$fields[2]["name"] = "displayorder";
	$fields[2]["title"] = $_SWIFT["language"]["tscatdisporder"];
	$fields[2]["width"] = "150";
	$fields[2]["align"] = "center";

	$fields[3]["type"] = "custom";
	$fields[3]["name"] = "options";
	$fields[3]["title"] = $_SWIFT["language"]["options"];
	$fields[3]["width"] = "150";
	$fields[3]["align"] = "center";

	$grid = new Grid();

	$interface->staffHeader($_SWIFT["language"]["troubleshooter"]." &gt; ".$_SWIFT["language"]["managecategories"], 6);

	$interface->staffNavBar('<a href="index.php?_m=troubleshooter&_a=managecategories" title="'.$_SWIFT["language"]["managecategories"].'">'.$_SWIFT["language"]["managecategories"]."</a>", "", 6);

	if ($_SWIFT["staff"]["tsmanagecategories"] == 1)
	{
		$grid->start("troubleshootercat", $_SWIFT["language"]["tscatlist"], 'SELECT * FROM `'. TABLE_PREFIX .'troubleshootercat` $_sortjoin;', 'SELECT COUNT(*) AS totalitems FROM `'. TABLE_PREFIX .'troubleshootercat`;', 'SELECT * FROM `'. TABLE_PREFIX .'troubleshootercat` WHERE `title` LIKE \'%$_searchstr%\' $_sortjoin;', $fields, $options);
	} else {
		$errormessage = $_SWIFT["language"]["nopermission"];
	}

	printInfoBox($infomessage);
	printErrorBox($errormessage);

	$grid->display("troubleshootercat");
	
	$interface->staffFooter();




/**
* ###############################################
* INSERT TROUBLESHOOTER CATEGORY
* ###############################################
*/
} else if ($eventaction == "insertcategory") {
	if ($_POST["step"] == 1)
	{
		if (trim($_POST["title"]) == "" || trim($_POST["displayorder"]) == "")
		{
			$errormessage = $_SWIFT["language"]["requiredfieldempty"];
		} else if (!count($_POST["tgroupid"])) {
			$errormessage = $_SWIFT["language"]["selectonetgroup"];
		} else if ($_SWIFT["staff"]["tsinsertcategory"] != 1) {
			$errormessage = $_SWIFT["language"]["nopermission"];
		} else {
			$_POST["displayorder"] = iif(empty($_POST["displayorder"]), "1", $_POST["displayorder"]);	

			insertTroubleshooterCat($_POST["title"], $_POST["description"], $_POST["displayorder"], $_POST["tgroupid"]);

			printRedirect(sprintf($_SWIFT["language"]["troubleshootercatinsertconfirm"], htmlspecialchars($_POST["title"])), "index.php?_m=troubleshooter&_a=managecategories&insertcat=".urlencode($_POST["title"]));
			exit;
		}
	}

	checkTRHtmlArea();

	$interface->staffHeader($_SWIFT["language"]["troubleshooter"]." &gt; ".$_SWIFT["language"]["insertcategory"], 6);

	$interface->staffNavBar('<a href="index.php?_m=troubleshooter&_a=managecategories" title="'.$_SWIFT["language"]["managecategories"].'">'.$_SWIFT["language"]["managecategories"].'</a> &raquo; '.'<a href="index.php?_m=troubleshooter&_a=insertcategory" title="'.$_SWIFT["language"]["insertcategory"].'">'.$_SWIFT["language"]["insertcategory"].'</a>', "", 6);

	printInfoBox($infomessage);
	printErrorBox($errormessage);

	renderTroubleshooterCatForm(INSERT);
	
	if ($_GET["back"] == "steps")
	{
		$template->assign("backurl", "index.php?_m=troubleshooter&_a=managesteps");
	} else {
		$template->assign("backurl", "index.php?_m=troubleshooter&_a=managecategories");
	}

	$interface->staffFooter();


/**
* ###############################################
* EDIT TROUBLESHOOTER CATEGORY
* ###############################################
*/
} else if ($eventaction == "editcategory") {
	// First get the category data
	$_troubleshootercat = $dbCore->queryFetch("SELECT * FROM `". TABLE_PREFIX ."troubleshootercat` AS troubleshootercat WHERE troubleshootercat.troubleshootercatid = '". intval($_REQUEST["troubleshootercatid"]) ."';");
	if (empty($_troubleshootercat["troubleshootercatid"]))
	{
		trigger_error($_SWIFT["language"]["invalidtroubleshootercategory"], E_USER_ERROR);
		exit;
	}

	if ($_POST["step"] == 1)
	{
		if (trim($_POST["title"]) == "" || trim($_POST["displayorder"]) == "")
		{
			$errormessage = $_SWIFT["language"]["requiredfieldempty"];
		} else if (!count($_POST["tgroupid"])) {
			$errormessage = $_SWIFT["language"]["selectonetgroup"];
		} else if ($_SWIFT["staff"]["tseditcategory"] != 1) {
			$errormessage = $_SWIFT["language"]["nopermission"];
		} else {
			$_POST["displayorder"] = iif(empty($_POST["displayorder"]), "1", $_POST["displayorder"]);
			$_POST["views"] = iif(empty($_POST["views"]), "1", $_POST["views"]);

			updateTroubleshooterCat($_troubleshootercat["troubleshootercatid"], $_POST["title"], $_POST["description"], $_POST["displayorder"], $_POST["views"], $_POST["tgroupid"]);

			printRedirect(sprintf($_SWIFT["language"]["troubleshootercatupdateconfirm"], htmlspecialchars($_POST["title"])), "index.php?_m=troubleshooter&_a=managecategories&updatecat=".urlencode($_POST["title"]));
			exit;
		}		
	}

	checkTRHtmlArea();

	$_POST = array_merge($_POST, $_troubleshootercat);

	$interface->staffHeader($_SWIFT["language"]["troubleshooter"]." &gt; ".$_SWIFT["language"]["editcategory"], 6);

	$interface->staffNavBar('<a href="index.php?_m=troubleshooter&_a=managecategories" title="'.$_SWIFT["language"]["managecategories"].'">'.$_SWIFT["language"]["managecategories"].'</a> &raquo; '.'<a href="index.php?_m=troubleshooter&_a=editcategory&troubleshootercatid='. $_troubleshootercat["troubleshootercatid"] .'" title="'.$_SWIFT["language"]["editcategory"].'">'.$_SWIFT["language"]["editcategory"].'</a>', "", 6);

	printInfoBox($infomessage);
	printErrorBox($errormessage);

	renderTroubleshooterCatForm(EDIT);
	
	$template->assign("backurl", "index.php?_m=troubleshooter&_a=managecategories");

	$interface->staffFooter();




/**
* ###############################################
* MANAGE TROUBLESHOOTER STEPS
* ###############################################
*/
} else if ($eventaction == "managesteps") {
	if ($_GET["do"] == "delete" && !empty($_GET["troubleshooterid"]))
	{
		if ($_SWIFT["staff"]["tsdeletestep"] == 1)
		{
			$infomessage = $_SWIFT["language"]["tsdelconfirm"];

			$totaldel = deleteSteps($_GET["troubleshooterid"]);

			printRedirect(sprintf($_SWIFT["language"]["stepdeleteconfirm"], htmlspecialchars($totaldel)), "index.php?_m=troubleshooter&_a=managesteps&stepdelcnt=".urlencode($totaldel));
			exit;
		} else {
			$errormessage = $_SWIFT["language"]["nopermission"];
		}
	}

	if (!empty($_REQUEST["stepinsert"]))
	{
		$infomessage = sprintf($_SWIFT["language"]["tsaddconfirm"], htmlspecialchars($_REQUEST["stepinsert"]));
	} else if (!empty($_REQUEST["stepdelcnt"])) {
		$infomessage = sprintf($_SWIFT["language"]["stepdeleteconfirm"], htmlspecialchars($_REQUEST["stepdelcnt"]));
	} else if (!empty($_REQUEST["stepupdate"])) {
		$infomessage = sprintf($_SWIFT["language"]["tsupdateconfirm"], htmlspecialchars($_REQUEST["stepupdate"]));		
	}
	

	require_once ("./includes/TreeMenu/TreeMenu.php");
	$template->assign("treemenu", true);

	$icon = 'icon_folderyellow3.gif';
	$expandedIcon = 'icon_folderyellow3.gif';

	$menu = new HTML_TreeMenu();

	// Do we need to filter it to one user group?
	$dofilter = false;
	if ($_REQUEST["filter"] == "1")
	{
		$dofilter = true;
	}

	// Ok, Now fetch all the categories we have
	$tscatlist = $tscatidlist = array();
	if ($dofilter == true && !empty($_REQUEST["filtertgroupid"]))
	{
		$dbCore->query("SELECT troubleshootercat.* FROM `". TABLE_PREFIX ."troubleshootercat` AS troubleshootercat LEFT JOIN `". TABLE_PREFIX ."tgroupassigns` AS tgroupassigns ON (troubleshootercat.troubleshootercatid = tgroupassigns.toassignid AND tgroupassigns.type = '". TGROUP_TROUBLESHOOTER ."') WHERE tgroupassigns.tgroupid = '". intval($_REQUEST["filtertgroupid"]) ."' ORDER BY troubleshootercat.displayorder ASC;");
	} else {
		$dbCore->query("SELECT * FROM `". TABLE_PREFIX ."troubleshootercat` ORDER BY `displayorder` ASC;");
	}
	while ($dbCore->nextRecord())
	{
		$doadd = false;

		// Is it filtered result?
		if ($dofilter == true && !empty($_REQUEST["troubleshootercatid"])) {
			if ($_REQUEST["troubleshootercatid"] == $dbCore->Record["troubleshootercatid"])
			{
				$doadd = true;
			}
		} else if ($dofilter == true && !empty($_REQUEST["filtertgroupid"])) {
			$doadd = true;
		} else if (!$dofilter) {
			// We always add the troubleshooter category to list if filtering isnt enabled
			$doadd = true;
		}

		if ($doadd == true)
		{
			$tscatlist[$dbCore->Record["troubleshootercatid"]] = $dbCore->Record;
			$tscatidlist[] = $dbCore->Record["troubleshootercatid"];
		}
	}

	// Build the entire troubleshooter data based on the category list. This will load all the links and troubleshooter data
	$categorydata = buildTSCategoryData($tscatidlist);

	// Now that we have all the categories, we need to build up their step tree. we do this by creating a parent node and calling the buildTSCategoryTreeData function to build the tree of steps for this specific category
	$catnodelist = array();

	$mainnode = new HTML_TreeNode(array('text' => "&nbsp;". $_SWIFT["language"]["troubleshooters"] ."&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[ <a href='index.php?_m=troubleshooter&_a=insertcategory&back=steps' id='blue' title='". $_SWIFT["language"]["addcategory"] ."'>". $_SWIFT["language"]["addcategory"] ."</a> ]", 'link' => "", 'icon' => "icon_list2.gif", 'expandedIcon' => "icon_list2.gif", 'expanded' => true));


	foreach ($tscatlist as $key=>$val)
	{
		$catnodecount = count($catnodelist);
	
		if (count($categorydata[$val["troubleshootercatid"]]))
		{
			$isexpanded = true;
		} else {
			$isexpanded = false;
		}

		// Create the parent node
		$catnodelist[$catnodecount] = new HTML_TreeNode(array('text' => "&nbsp;". $val["title"] ."&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[ <a href='index.php?_m=troubleshooter&_a=insertstep&troubleshootercatid=". $val["troubleshootercatid"] ."' id='blue' title='". $_SWIFT["language"]["addstep"] ."'>". $_SWIFT["language"]["addstep"] ."</a> ]", 'link' => "", 'icon' => $icon, 'expandedIcon' => $expandedIcon, 'expanded' => $isexpanded));

		// Build all its steps!
		doTSCategoryTreeLoop($val["troubleshootercatid"], $categorydata, 0, $catnodelist[$catnodecount], 0);

		$mainnode->addItem($catnodelist[$catnodecount]);
	}

	// Add the main node to our menu
	$menu->addItem($mainnode);

	// Create the presentation class
	$treeMenu = &new HTML_TreeMenu_DHTML($menu, array('images' => stripTrailingSlash($_SWIFT["themepath"]), 'defaultClass' => 'treeMenuDefault'));

	$interface->staffHeader($_SWIFT["language"]["troubleshooter"]." &gt; ".$_SWIFT["language"]["managesteps"], 6);

	$interface->staffNavBar('<a href="index.php?_m=troubleshooter&_a=managecategories" title="'.$_SWIFT["language"]["managecategories"].'">'.$_SWIFT["language"]["managecategories"].'</a> &raquo; '.'<a href="index.php?_m=troubleshooter&_a=managesteps" title="'.$_SWIFT["language"]["managesteps"].'">'.$_SWIFT["language"]["managesteps"].'</a>', "", 6);

	if ($_SWIFT["staff"]["tsmanagesteps"] != 1)
	{
		$errormessage = $_SWIFT["language"]["nopermission"];
	}
	printInfoBox($infomessage);
	printErrorBox($errormessage);

	if ($_SWIFT["staff"]["tsmanagesteps"] == 1)
	{
		// ======= FILTER =======
		echo '<div id="filtertscat" style="DISPLAY:none;">'.SWIFT_CRLF;
		printFormStart();
		printMainTableHeader($_SWIFT["language"]["filter"]);

		$options = array();
		$index = 0;
		$dbCore->query("SELECT * FROM `". TABLE_PREFIX ."templategroups` ORDER BY `title` ASC;");
		while ($dbCore->nextRecord())
		{
			$options[$index]["title"] = $dbCore->Record["title"];
			$options[$index]["value"] = $dbCore->Record["tgroupid"];
			$index++;
		}

		printSelectRow("filtertgroupid", $_SWIFT["language"]["filtertgroupid"], $_SWIFT["language"]["desc_filtertgroupid"], $options);
		printSubmitRow($_SWIFT["language"]["filter"]);
		printMainTableFooter();
		addHiddenField("_m", "troubleshooter");
		addHiddenField("_a", "managesteps");
		addHiddenField("filter", "1");
		printFormEnd();
		echo '<BR /></div>'.SWIFT_CRLF;	


		printMainTableHeader($_SWIFT["language"]["troubleshootersteps"]);
		// ======= TOOLBAR OPTIONS =======
		echo '<tr>'.SWIFT_CRLF;
		echo '<td class="toolbarrow" align="left" colspan="2" valign="middle" nowrap><table width="100%" border="0" cellspacing="0" cellpadding="0"><tr><td width="5"><img src="'. $_SWIFT["themepath"] .'grip.gif" border="0" /></td><td class="toolbarrow"><span class="toolbartext">&nbsp;<a href="index.php?_m=troubleshooter&_a=managestepslist" id="white" title="'. $_SWIFT["language"]["listview"] .'">'. $_SWIFT["language"]["listview"] .'</a>&nbsp;|&nbsp;<a href="javascript:switchDisplay(\'filtertscat\');" id="white" title="'. $_SWIFT["language"]["filter"] .'">'. $_SWIFT["language"]["filter"] .'</a></span></td></tr></table></td>'.SWIFT_CRLF;
		echo '</tr>'.SWIFT_CRLF;
		// ======= TOOLBAR OPTIONS =======

		ob_start();
		?><div style="position:relative; width:100%; height:320px; background-color:#ffffff; overflow:auto;"><?
		$treeMenu->printMenu();
		?></div><?
		$contents = ob_get_contents();
		ob_end_clean();

		printDescRow($contents, "2", "defaultrow");

		printMainTableFooter();
	}

	$template->assign("backurl", "index.php?_m=troubleshooter&_a=managecategories");

	$interface->staffFooter();




/**
* ###############################################
* INSERT TROUBLESHOOTER STEP
* ###############################################
*/
} else if ($eventaction == "insertstep") {
	// Get the details on the troubleshooter category
	$_troubleshootercat = $dbCore->queryFetch("SELECT * FROM `". TABLE_PREFIX ."troubleshootercat` WHERE `troubleshootercatid` = '". intval($_REQUEST["troubleshootercatid"]) ."';");
	if (empty($_troubleshootercat["troubleshootercatid"]))
	{
		trigger_error($_SWIFT["language"]["invalidtroubleshootercategory"], E_USER_ERROR);
		exit;
	}

	if ($_POST["step"] == "1")
	{
		if (trim($_POST["subject"]) == "" || trim($_POST["displayorder"]) == "" || trim($_POST["contents"]) == "")
		{
			$errormessage = $_SWIFT["language"]["requiredfieldempty"];
		} else if (!count($_POST["links"])) {
			$errormessage = $_SWIFT["language"]["selectonelink"];
		} else if ($_SWIFT["staff"]["tsaddstep"] != 1) {
			$errormessage = $_SWIFT["language"]["nopermission"];
		} else {
			// Ok proceed with adding the troubleshooter
			$_POST["displayorder"] = iif(empty($_POST["displayorder"]), "1", $_POST["displayorder"]);	

			$troubleshooterid = insertStep($_troubleshootercat["troubleshootercatid"], $_POST["subject"], $_POST["displayorder"], $_POST["contents"], $_POST["links"]);

			printRedirect(sprintf($_SWIFT["language"]["tsaddconfirm"], htmlspecialchars($_POST["subject"])), "index.php?_m=troubleshooter&_a=managesteps&stepinsert=".urlencode($_POST["subject"]));
			exit;
		}		
	}

	checkTRHtmlArea();

	$interface->staffHeader($_SWIFT["language"]["troubleshooter"]." &gt; ".$_SWIFT["language"]["managesteps"], 6);

	$interface->staffNavBar('<a href="index.php?_m=troubleshooter&_a=managecategories" title="'.$_SWIFT["language"]["managecategories"].'">'.$_SWIFT["language"]["managecategories"].'</a> &raquo; <a href="index.php?_m=troubleshooter&_a=managesteps" title="'.$_SWIFT["language"]["managesteps"].'">'.$_SWIFT["language"]["managesteps"].'</a> &raquo; <a href="index.php?_m=troubleshooter&_a=insertstep&troubleshootercatid='. $_troubleshootercat["troubleshootercatid"] .'" title="'.$_SWIFT["language"]["insertstep"].'">'.$_SWIFT["language"]["insertstep"].'</a>', "", 6);

	renderStepForm(INSERT, $_troubleshootercat);

	$template->assign("backurl", "index.php?_m=troubleshooter&_a=managesteps");

	$interface->staffFooter();







/**
* ###############################################
* EDIT TROUBLESHOOTER STEP
* ###############################################
*/
} else if ($eventaction == "editstep") {
	// Get the details on the troubleshooter category
	$_troubleshooter = $dbCore->queryFetch("SELECT * FROM `". TABLE_PREFIX ."troubleshootersteps` AS troubleshootersteps LEFT JOIN `". TABLE_PREFIX ."troubleshooterdata` AS troubleshooterdata ON (troubleshootersteps.troubleshooterid = troubleshooterdata.troubleshooterid) WHERE troubleshootersteps.troubleshooterid = '". intval($_REQUEST["troubleshooterid"]) ."';");
	if (empty($_troubleshooter["troubleshooterid"]))
	{
		trigger_error($_SWIFT["language"]["invalidtroubleshooter"], E_USER_ERROR);
		exit;
	}

	$_troubleshootercat = $dbCore->queryFetch("SELECT * FROM `". TABLE_PREFIX ."troubleshootercat` WHERE `troubleshootercatid` = '". intval($_troubleshooter["troubleshootercatid"]) ."';");
	if (empty($_troubleshootercat["troubleshootercatid"]))
	{
		trigger_error($_SWIFT["language"]["invalidtroubleshootercategory"], E_USER_ERROR);
		exit;
	}

	$_links = array();
	$dbCore->query("SELECT * FROM `". TABLE_PREFIX ."troubleshooterlinks` WHERE `childtroubleshooterid` = '". intval($_troubleshooter["troubleshooterid"]) ."';");
	while ($dbCore->nextRecord())
	{
		$_links[] = $dbCore->Record["parenttroubleshooterid"];
	}

	if ($_POST["step"] == "1")
	{
		if (trim($_POST["subject"]) == "" || trim($_POST["displayorder"]) == "" || trim($_POST["contents"]) == "")
		{
			$errormessage = $_SWIFT["language"]["requiredfieldempty"];
		} else if (!count($_POST["links"])) {
			$errormessage = $_SWIFT["language"]["selectonelink"];
		} else if ($_SWIFT["staff"]["tseditstep"] != 1) {
			$errormessage = $_SWIFT["language"]["nopermission"];
		} else {
			// Ok proceed with updating the troubleshooter
			$_POST["displayorder"] = iif(empty($_POST["displayorder"]), "1", $_POST["displayorder"]);	

			updateStep($_troubleshooter["troubleshooterid"], $_troubleshootercat["troubleshootercatid"], $_POST["subject"], $_POST["displayorder"], $_POST["contents"], $_POST["links"]);

			printRedirect(sprintf($_SWIFT["language"]["tsupdateconfirm"], htmlspecialchars($_POST["subject"])), "index.php?_m=troubleshooter&_a=managesteps&stepupdate=".urlencode($_POST["subject"]));
			exit;
		}
	}
	$_POST = array_merge($_POST, $_troubleshooter);

	checkTRHtmlArea();

	$interface->staffHeader($_SWIFT["language"]["troubleshooter"]." &gt; ".$_SWIFT["language"]["updatestep"], 6);

	$interface->staffNavBar('<a href="index.php?_m=troubleshooter&_a=managecategories" title="'.$_SWIFT["language"]["managecategories"].'">'.$_SWIFT["language"]["managecategories"].'</a> &raquo; <a href="index.php?_m=troubleshooter&_a=managesteps" title="'.$_SWIFT["language"]["managesteps"].'">'.$_SWIFT["language"]["managesteps"].'</a> &raquo; <a href="index.php?_m=troubleshooter&_a=editstep&troubleshooterid='. $_troubleshooter["troubleshooterid"] .'" title="'.$_SWIFT["language"]["editstep"].'">'.$_SWIFT["language"]["editstep"].'</a>', "", 6);

	renderStepForm(EDIT, $_troubleshootercat, $_links);

	$template->assign("backurl", "index.php?_m=troubleshooter&_a=managesteps");

	$interface->staffFooter();








/**
* ###############################################
* IMPORT/EXPORT
* ###############################################
*/
} else if ($eventaction == "importexport") {
	require_once ("./includes/functions_xml.php");
	require_once ("./modules/troubleshooter/functions_importexport.php");
	$xml = new XML;

	if ($_POST["step"] == "export")
	{
		// Export this thing
		if ($_SWIFT["staff"]["tsexport"] == 1)
		{
			exportCategories($_POST["troubleshootercatid"]);
			exit;
		} else {
			$errormessage = $_SWIFT["language"]["nopermission"];
		}
	} else if ($_POST["step"] == "import") {
		// Import the data
		if ($_SWIFT["staff"]["tsimport"] == 1)
		{
			if (file_exists($_FILES["troubleshooterfile"]["tmp_name"]))
			{
				importTroubleshooter($_FILES["troubleshooterfile"]["tmp_name"]);

				printRedirect($_SWIFT["language"]["importconfirm"], "index.php?_m=troubleshooter&_a=managesteps");
				exit;
			}
		} else {
			$errormessage = $_SWIFT["language"]["nopermission"];
		}
	}

	$interface->staffHeader($_SWIFT["language"]["troubleshooter"]." &gt; ".$_SWIFT["language"]["importexport"], 6);

	$interface->staffNavBar('<a href="index.php?_m=troubleshooter&_a=importexport" title="'.$_SWIFT["language"]["importexport"].'">'.$_SWIFT["language"]["importexport"].'</a>', "", 6);

	printInfoBox($infomessage);
	printErrorBox($errormessage);


	$options = array();
	$index = 0;
	$dbCore->query("SELECT * FROM `". TABLE_PREFIX ."troubleshootercat` ORDER BY `displayorder` ASC;");
	while ($dbCore->nextRecord())
	{
		$options[$index]["value"] = $dbCore->Record["troubleshootercatid"];
		$options[$index]["title"] = $dbCore->Record["title"];
		$options[$index]["checked"] = true;
		$index++;
	}

	if (count($options))
	{
		printFormStart();
		printMainTableHeader($_SWIFT["language"]["export"]);
		printCheckboxListRow("troubleshootercatid", $_SWIFT["language"]["exportcat"], $_SWIFT["language"]["desc_exportcat"], $options);

		printSubmitRow($_SWIFT["language"]["exportxml"]);

		printMainTableFooter();

		addHiddenField("_m", "troubleshooter");
		addHiddenField("_a", "importexport");
		addHiddenField("step", "export");
		printFormEnd();
		echo "<BR />";
	}

	// ======= IMPORT TROUBLESHOOTER =======
	printFormStart("importform", "index.php", "POST", true);
	printMainTableHeader($_SWIFT["language"]["importtroubleshooter"]);

	printUploadRow("troubleshooterfile", $_SWIFT["language"]["troubleshooterfile"], $_SWIFT["language"]["desc_troubleshooterfile"]);

	printSubmitRow($_SWIFT["language"]["importxml"]);

	printMainTableFooter();

	addHiddenField("_m", "troubleshooter");
	addHiddenField("_a", "importexport");
	addHiddenField("step", "import");
	printFormEnd();
	

	$template->assign("backurl", "index.php?_m=troubleshooter&_a=managecategories");

	$interface->staffFooter();



/**
* ###############################################
* MANAGE STEPS LIST VIEW
* ###############################################
*/
} else if ($eventaction == "managestepslist") {
	if ($_GET["do"] == "delete" && !empty($_GET["troubleshooterid"]))
	{
		if ($_SWIFT["staff"]["tsdeletestep"] == 1)
		{
			$infomessage = $_SWIFT["language"]["tsdelconfirm"];

			$totaldel = deleteSteps($_GET["troubleshooterid"]);

			printRedirect(sprintf($_SWIFT["language"]["stepdeleteconfirm"], htmlspecialchars($totaldel)), "index.php?_m=troubleshooter&_a=managestepslist&stepdelcnt=".urlencode($totaldel));
			exit;
		} else {
			$errormessage = $_SWIFT["language"]["nopermission"];
		}
	}

	if (!empty($_REQUEST["stepdelcnt"])) {
		$infomessage = sprintf($_SWIFT["language"]["stepdeleteconfirm"], htmlspecialchars($_REQUEST["stepdelcnt"]));
	}

	$options["recordsperpage"] = "8";
	$options["sortby"] = "troubleshootersteps.displayorder";
	$options["sortorder"] = "asc";
	$options["massaction"][0]["title"] = $_SWIFT["language"]["delete"];
	$options["massaction"][0]["callback"] = "_maDeleteTroubleshooterStep";

	$options["idname"] = "troubleshooterid";
	$options["quicksearch"] = true;

	$options["advancedsearch"][0]["title"] = $_SWIFT["language"]["tssubject"];
	$options["advancedsearch"][0]["query"] = 'SELECT * FROM `'. TABLE_PREFIX .'troubleshootersteps` AS troubleshootersteps WHERE troubleshootersteps.subject LIKE \'%$_searchstr%\' $_sortjoin;';

	$options["advancedsearch"][1]["title"] = $_SWIFT["language"]["contents"];
	$options["advancedsearch"][1]["query"] = 'SELECT * FROM `'. TABLE_PREFIX .'troubleshootersteps` AS troubleshootersteps LEFT JOIN `'. TABLE_PREFIX .'troubleshooterdata` AS troubleshooterdata ON (troubleshootersteps.troubleshooterid = troubleshooterdata.troubleshooterid) WHERE troubleshooterdata.contents LIKE \'%$_searchstr%\' $_sortjoin;';

	$options["callback"] = "_troubleshooterStepFields";

	$options["toolbar"][0]["title"] = $_SWIFT["language"]["treeview"];
	$options["toolbar"][0]["url"] = "index.php?_m=troubleshooter&_a=managesteps";

	$fields[0]["name"] = "troubleshootersteps.subject";
	$fields[0]["title"] = $_SWIFT["language"]["tssubject"];
	$fields[0]["width"] = "";

	$fields[1]["name"] = "troubleshootersteps.displayorder";
	$fields[1]["title"] = $_SWIFT["language"]["tsdisporder"];
	$fields[1]["width"] = "150";
	$fields[1]["align"] = "center";

	$fields[2]["type"] = "custom";
	$fields[2]["name"] = "options";
	$fields[2]["title"] = $_SWIFT["language"]["options"];
	$fields[2]["width"] = "150";
	$fields[2]["align"] = "center";

	$grid = new Grid();

	$interface->staffHeader($_SWIFT["language"]["troubleshooter"]." &gt; ".$_SWIFT["language"]["managesteps"], 6);

	$interface->staffNavBar('<a href="index.php?_m=troubleshooter&_a=managecategories" title="'.$_SWIFT["language"]["managecategories"].'">'.$_SWIFT["language"]["managecategories"].'</a> &raquo; '.'<a href="index.php?_m=troubleshooter&_a=managesteps" title="'.$_SWIFT["language"]["managesteps"].'">'.$_SWIFT["language"]["managesteps"].'</a>', "", 6);

	if ($_SWIFT["staff"]["tsmanagesteps"] == 1)
	{
		$grid->start("troubleshootersteps", $_SWIFT["language"]["tssteplist"], 'SELECT * FROM `'. TABLE_PREFIX .'troubleshootersteps` AS troubleshootersteps $_sortjoin;', 'SELECT COUNT(*) AS totalitems FROM `'. TABLE_PREFIX .'troubleshootersteps` AS troubleshootersteps;', 'SELECT * FROM `'. TABLE_PREFIX .'troubleshootersteps` AS troubleshootersteps WHERE troubleshootersteps.subject LIKE \'%$_searchstr%\' $_sortjoin;', $fields, $options);
	} else {
		$errormessage = $_SWIFT["language"]["nopermission"];
	}

	printInfoBox($infomessage);
	printErrorBox($errormessage);

	
	$grid->display("troubleshootersteps");
	
	$interface->staffFooter();
}
?>